package com.css.zfzx.sjcj.common.division;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.org.dept.repository.entity.DeptEntity;
import com.css.bpm.platform.org.division.repository.DivisionRepository;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.org.role.repository.entity.RoleEntity;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.sjcj.common.utils.ServerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author leon
 * @Title:
 * @Description:
 * @date 2020/11/4 16:20
 */
@RestController
@Slf4j
@PropertySource("classpath:platform-config.yml")
public class Division {
    /*@Autowired
    DivisionRepository divisionRepository;
    @Value("${role.superCode}")
    private String superCode;*/
    /**
     * 行政区划
     *
     * @return
     */
    @GetMapping("divisions/{divisionid}/subdivisions")
    public RestResponse getDisvion(@PathVariable("divisionid") String divisionId) {
        RestResponse restResponse = null;
        try {
            List<DivisionEntity> subDivisions = PlatformAPI.getDivisionAPI().getSubDivisions(divisionId);
            restResponse = RestResponse.succeed(subDivisions);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

//    public RestResponse userDivision() {
//        JSONObject division = new JSONObject();
//        String userId = PlatformSessionUtils.getUserId();
//        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
//        // 是否为超级管理员
//        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
//        DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
//        if ("root".equals(mainDept.getDeptId()) || containtRole) {
//            division.put("message", "超级管理员");
//        } else {
//            division.put("message", "非超级管理员");
//            String divisionId = mainDept.getDivisionId();
//            DivisionEntity divisionEntity = divisionRepository.findValidDivisionById(divisionId);
//            String divisionName = divisionEntity.getDivisionName();
//            // 省
//            if (divisionName.contains("省") || divisionName.contains("自治区") || divisionName.equals("北京市")
//                    || divisionName.equals("天津市") || divisionName.equals("上海市") || divisionName.equals("重庆市")) {
//                division.put("divisionId", divisionId);
//                division.put("divisionName", divisionName);
//            } else { // 区，获取父
//                String parentId = divisionEntity.getParentId();
//                DivisionEntity divisionParentEntity = divisionRepository.findValidDivisionById(parentId);
//                String divisionParentName = divisionParentEntity.getDivisionName();
//                // 父的地区名称和ID
//                division.put("divisionId", parentId);
//                division.put("divisionName", divisionParentName);
//                // 地区名称和ID
//                division.put("cityDivisionId", divisionId);
//                division.put("cityDivisionName", divisionName);
//            }
//        }
//        return RestResponse.succeed(division);
//    }
}
